import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

import Fk.Common
import Fk.Pages
import Fk.RoomElement
import "Pokeutil.js" as Pokeutil

GraphicsBox  {
    id:root
    //visible: true
    width: 600
    height: 500
    
    //title: "商店模块"
    //["common","super","advanced","heart","advantage","master"]
    property string picpath:AppPath+"/packages/jbs2/image/gem/"
    property bool iscurrent:false
    property list<int> selected_gems:[0,0,0,0,0,0]
    property var shop_cards:[
      ["poke__yibu","poke__xiaohuolong","poke__miaowazhongzi","poke__xiaohuolong"],
      ["poke__yibu","poke__xiaohuolong","poke__miaowazhongzi","poke__xiaohuolong"],
      ["poke__yibu","poke__xiaohuolong","poke__miaowazhongzi","poke__xiaohuolong"]
    ]

    property list<int> total_gems:[5,5,5,5,5,5]
    property list<string> selected_cards: []
    property list<int> player_gems:[7,7,7,7,7,7]

    ListView {
      id: cardView
      //anchors.fill: parent
      anchors.topMargin: 40
      anchors.leftMargin: 20
      anchors.rightMargin: 20
      anchors.bottomMargin: 30
      spacing: 20
      model: 4
      height:400
      width:500
      clip: true
      //orientation:Qt.Horizontal

      delegate:RowLayout {

          spacing: 10
          property int outindex:index
          property var shopselect_cards:[]
          /*
          Rectangle {
            id:levelshow
            border.color: "#A6967A"
            radius: 5
            color: "transparent"
            width: 18
            height: 160
            Layout.alignment: Qt.AlignTop

            Text {
              color: "#E4D5A0"
              text: index===0 ? "传说级" :"等级"+index
              anchors.fill: parent
              wrapMode: Text.WrapAnywhere
              verticalAlignment: Text.AlignVCenter
              horizontalAlignment: Text.AlignHCenter
              font.pixelSize: 15
            }
          }
          */
          
          PokecardItem {

           picsource: picpath+"pokemon/"+"level"+outindex
           //autoBack: false
           //known: model.cid !== -1
           //selectable: chosenInBox
           selectable: false

          }
          
         ListView {
             id:innercardlistview
            //columns: 6
            //id:outid
            orientation:Qt.Horizontal
            width:600
            //anchors.fill: levelcard
            //anchors.left: levelcard.right
            anchors.leftMargin: 20
            //Layout.alignment: Qt.AlignLeft
            model: 5

            spacing: 10
            clip: true
            height:200
            delegate:PokecardItem {

                name:""+shop_cards[outindex][index]
                picsource:picpath+"pokemon/"+ name 
                chosenInBox: root.selected_cards.includes(name)
                //chosenInBox: true
                //autoBack: false
                //known: model.cid !== -1
                selectable: !chosenInBox 

                //selectable:index!== 0

                onClicked: {
                  if (selected) {
                      root.selected_cards.push(name);
                      chosenInBox= root.selected_cards.includes(name);
                      checkis()

                   }
                    //chosenInBox = false;
                    //root.selected_ids.splice(root.selected_ids.indexOf(cid), 1);
                  }



                  //root.selected_ids = root.selected_ids;
                  //refreshPrompt();
              }


            }

      }
    }







    Row{
      id:balllist
      anchors.margins: 8
      anchors.top: cardView.bottom
      //anchors.horizontalCenter: parent.horizontalCenter
        spacing:20
        Repeater {

          id: ballRepeater
          model: ["common","super","advanced","heart","advantage","master"]
          TokenItem {
            pwidth: 60
            pheight: 60

            selectdnum:root.selected_gems[index]
            total:total_gems[index]
            picsource: picpath+ "ball/"+modelData
            //autoBack: false
            //known: model.cid !== -1
            //selectable: chosenInBox
            //enabled:total>0
            onClicked: {
              if (total>0){
                //console.log("确认2")
                total=total-1;

                selectdnum=selectdnum+1;
                Pokeutil.updategems(root.selected_gems,index)
                    checkis()
              }


            }
            onTotalChanged: {
              //selectdnum=selectdnum+1;
              //Pokeutil.updategems(root.selected_gems,index)


              //root.selected_ids = root.selected_ids;
              //refreshPrompt();
            }

          }
        }

    }

    Column {
            anchors.left: cardView.right
            anchors.verticalCenter: parent.verticalCenter
            spacing: 10
            visible: iscurrent
              //model: ["预定","确认","清空"]
            MetroButton {
               id:chosebunone
              Layout.alignment: Qt.AlignHCenter
              text: "预定"
              textFont.pixelSize: 12
              width: 50
              height: 40
              enabled:root.selected_cards.length>1
              onClicked: {

                close();
                //roomScene.state = "notactive";
              }
            }
            MetroButton {
              id:chosebuntwo
              Layout.alignment: Qt.AlignHCenter
              text: "确认"
              textFont.pixelSize: 12
              width: 50
              height: 40
              enabled:root.selected_cards.length>2
              onClicked: {
                  close();
                  roomScene.state = "notactive";
                ClientInstance.notifyServer("PushRequest", "updatemini,confirm");

                 ClientInstance.replyToServer("", JSON.stringify([Pokeutil.getcardintro(root.selected_cards),root.selected_gems]));
              
                //roomScene.state = "notactive";
              }
            }
            MetroButton {
              Layout.alignment: Qt.AlignHCenter
              text: "重选"
              id:chosebunthree
              textFont.pixelSize: 12
              width: 50
              height: 40
              enabled:true
              onClicked: {
               
                rechoose()

              }
            }


    }


    function checkis(){
         chosebuntwo.enabled=Pokeutil.isActionValid(root.selected_cards,root.selected_gems,player_gems);
         chosebunone.enabled=Pokeutil.ismasterball(root.selected_cards,root.selected_gems);
    }
    
    function rechoose(){

        root.selected_cards=[];
        root.selected_gems=[0,0,0,0,0,0];
        for (let i = 0; i < ballRepeater.count; ++i) {
                 ballRepeater.itemAt(i).selectdnum=root.selected_gems[i];
                 ballRepeater.itemAt(i).total= total_gems[i]-ballRepeater.itemAt(i).selectdnum;
        }
        //console.log(cardlistview.count)
        /*
        for (let j = 0;j < cardView.count; ++j) {
            var cardlist=cardView.itemAt(j).innercardlistview
            for (let i = 0; i < cardlist.count; ++i) {
                var card=cardlist.itemAt(i)
                card.chosenInBox= root.selected_cards.includes(card.name);
            }
        }
        */
        cardView.model=3;
       cardView.model=4;
    }

 function updateData(data) {
    const d = data;
    if (d.length == 0) {
      close();
      roomScene.state = "notactive";
      ClientInstance.replyToServer("", "");
    }
 }

  function loadData(data) {
    //console.log("hello")
    total_gems=data[0];
    player_gems=data[1];
    shop_cards=data[2];
    iscurrent=data[3];
    //cardView.model=shop_cards.length;
  }

}

